<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Login
 *
 * @author hassanali
 */
class dashboard extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/currentadmin_model', 'admin');
    }

    public function index() {
        $data = array(
            'email' => 'mtjarstatistics@gmail.com',
            'password' => '0123698745');
        $this->load->library('gapi', $data);

        $profileId = 76937975;
        $metrics = array('pageviews', 'visits');
        $filter = null;
        $startDate = date('Y-m-d', time() - 3600 * 24 * 15);
        $endDate = date('Y-m-d', time());
        $startIndex = 1;
        $maxResults = 10000;

        $dimensions = array('date');
        $sortMetric = array('date');
        $this->gapi->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);
        $this->pass['viewsbyday'] = $this->gapi->getResults();
        
        $this->pass['totalpageviews'] = $this->gapi->getPageviews();

        $metrics = array('visits');
        $dimensions = array('country');
        $sortMetric = array('country');
        $this->gapi->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);
        $this->pass['viewsbycountry'] = $this->gapi->getResults();
        
        $this->pass['totalvisits'] = $this->gapi->getVisits();
        
        $metrics = array('visitors');
        $dimensions = array('country');
        $sortMetric = array('country');
        $this->gapi->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);
        $this->pass['visitorsbycountry'] = $this->gapi->getResults();
        
        $this->pass['totalvisitors'] = $this->gapi->getVisitors();
        
        $this->load->model('admin/invoicies_model');
        $temp = $this->invoicies_model->get_stat();
        foreach($temp as $key => $value)
            $this->pass[$key] = $value;
        
        $this->load->model('admin/stores_model');
        $temp = $this->stores_model->get_stat();
        foreach($temp as $key => $value)
            $this->pass[$key] = $value;
        
        $this->load->model('admin/products_model');
        $temp = $this->products_model->get_stat();
        foreach($temp as $key => $value)
            $this->pass[$key] = $value;
        
        $this->load->model('admin/user_model');
        $temp = $this->user_model->get_stat();
        foreach($temp as $key => $value)
            $this->pass[$key] = $value;
        
        $this->themes->render('dashboard', $this->pass);
    }

    public function login() {
        if ($this->input->post()) {
            $this->form_validation->set_rules('username', lang('label_username'), 'trim|required|is_found[user.username]');
            $this->form_validation->set_rules('password', lang('label_password'), 'trim|required|min_length[6]');

            if ($this->form_validation->run()) {

                $username = $this->input->post('username');
                $password = $this->input->post('password');
                $this->user_data = $this->admin->login($username, $password);
                if ($this->user_data) {
                    $contant = array('username' => $username, 'password' => $password, 'ip' => $this->input->ip_address(), 'browser' => $this->input->user_agent(), 'login' => 1, 'mode' => $this->user_data->mode);
                    $this->admin->add_manager_report(array('action' => 'user-login', 'user_id' => $this->user_data->id, 'contant' => $contant));
                    $this->login_done('admin', 'dashboard');
                } else {
                    $contant = array('username' => $username, 'password' => $password, 'login' => 0, 'ip' => $this->input->ip_address(), 'browser' => $this->input->user_agent());
                    $this->admin->add_manager_report(array('action' => 'user-login', 'contant' => $contant));
                    $this->themes->set_message(array('url' => 'dashboard/login', 'type' => 'error', 'message' => 'message_login_faild'));
                }
            } else {
                get_validation_errors();
            }
        }

        $this->load->view('admin/login');
    }

    public function logout() {

        if (empty($this->admin_id)) {
            redirect(ADMIN_FOLDER . '/dashboard/login');
        } else {
            $contant = array('username' => $this->session->userdata('admin_NAME'), 'ip' => $this->session->userdata('ip_address'), 'browser' => $this->session->userdata('user_agent'));
            $this->admin->add_manager_report(array('action' => 'user-logout', 'contant' => $contant));
            $this->destroy_session('admin');
        }
    }

    function my_data() {
        if ($id = $this->admin_id) {
            $this->pass['admin'] = $data = $this->admin->is_found(array('id' => $id), array(), 'row', ADMIN_FOLDER . '/' . __CLASS__ . '/logout');

            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->load->library('form_validation');

                $this->form_validation->set_rules('fname', lang('label_fname'), 'required');
                $this->form_validation->set_rules('gender', lang('label_gender'), 'required|callback_gender_check');
                $this->form_validation->set_rules('day', lang('label_birthday'), 'required|is_natural_no_zero');
                $this->form_validation->set_rules('month', lang('label_birthday'), 'required|is_natural_no_zero');
                $this->form_validation->set_rules('year', lang('label_birthday'), 'required|is_natural_no_zero');
                $this->form_validation->set_rules('city', lang('label_city'), 'required|is_natural_no_zero');
                $this->form_validation->set_rules('mobile', lang('label_mobile'), 'required|is_natural');

                if ($data->username != $this->input->post('username'))
                    $this->form_validation->set_rules('username', lang('label_username'), 'trim|required|is_unique[user.username]|min_length[4]');
                if ($data->email != $this->input->post('email'))
                    $this->form_validation->set_rules('email', lang('label_email'), 'trim|required|is_unique[user.email]|valid_email');
                if ($this->input->post('password')) {
                    $this->form_validation->set_rules('oldpassword', lang('label_old_password'), 'trim|required|callback_oldpass_check');
                    $this->form_validation->set_rules('password', lang('label_password'), 'trim|required|min_length[4]');
                    $this->form_validation->set_rules('confpassword', lang('label_confpassword'), 'trim|required|min_length[4]|matches[password]');
                }

                if ($this->form_validation->run()) {
                    $save['first_name'] = $this->input->post('fname');
                    $save['middle_name'] = $this->input->post('mname');
                    $save['last_name'] = $this->input->post('lname');
                    $save['gender'] = strtoupper(substr($this->input->post('gender'), 0, 2));
                    $save['day'] = $this->input->post('day');
                    $save['month'] = $this->input->post('month');
                    $save['year'] = $this->input->post('year');
                    $save['city_id'] = $this->input->post('city');
                    $save['mobile'] = $this->input->post('mobile');
                    $save['email'] = $this->input->post('email');
                    $save['username'] = $this->input->post('username');

                    if ($this->input->post('password'))
                        $save['password'] = md5($this->input->post('password'));

                    if ($this->admin->save($save, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('type' => 'success', 'url' => __CLASS__, 'message' => 'message_save_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            }

            $this->load->model('admin/cities_model', 'cities');
            $this->pass['cities'] = $this->cities->get();

            $this->themes->render('mydata', $this->pass);
        }
        else
            redirect($this->agent->refrrer());
    }

}